package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/checkItem")
@Slf4j
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 检查组分页查询请求：页码+条数+查询条件
     *检查组分页查询响应： 总条数+展示数据的集合
     * @param currentPage 当前页面页号
     * @param pageSize    当前页面条数
     * @param queryString        按条件查询
     */
    @GetMapping("/page")
    public R<Page<CheckItem>> page(int currentPage, int pageSize, String queryString) {
        Page<CheckItem> pageInfo = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!queryString.equals("null"), CheckItem::getName, queryString).or().eq(!queryString.equals("null"), CheckItem::getCode, queryString);
        wrapper.orderByAsc(CheckItem::getCode);
        Page<CheckItem> pageList = checkItemService.page(pageInfo, wrapper);
        return R.success(pageList);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody CheckItem checkItem) {
        log.info("新增checkItem{}",checkItem);
        checkItemService.save(checkItem);
        return R.success("新增成功");
    }

    @DeleteMapping("/delete")
    public R<String> delete(int id){
        checkItemService.removeById(id);
        return R.success("删除成功");
    }

    @PutMapping("/update")
    public R<String> update(@RequestBody CheckItem checkitem){
        checkItemService.updateById(checkitem);
        return R.success("修改成功");
    }
}
